﻿<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.10.3.custom.min.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />						<!-- 本專案自行定義 CSS -->
<script src="js/jquery-1.9.1.min.js"></script>		<!-- jQuery 核心 -->
<script src="js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>								<!-- jqueryUI 核心 -->
<script src="js/jquery.cookie.js"></script>																	<!-- 操作cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script>												<!-- 本專案自行定義 JS -->

<title>提領</title>

<script type="text/javascript">

var tmp = "";

function startDep(sn) {
	openLoading("提領中...");
	$.post("Withdraw",{subSysId:$("#subSysId").val(),loginUser:$.cookie("userId"),deliverSN_q:sn,tarSysNo:$("select option:selected").val(),delTaskComment:$("#comment").val()},function(reses) {
		$(".localVer").html( reses.ver );
		$("#wdButton").html("已提領").button().prop("disabled",true);		
		closeLoading();
	},"JSON");
}

/* 轉換畫面至該筆交付事件的內容以進行部署 */
function enDeliTask(sn,statusSN) {
	$("#removeQueryResHook").remove();
	
	tmp = "<div id='removeQueryResHook'><hr/><p><br><button type='button' id='wdButton' onclick=\"startDep('"+sn+"')\">開始提領</button><p><p><textarea id='comment' readonly='readonly'></textarea><p><div id='hookGFS'></div><p><p><div id='hookRDS'></div></div>";
	$("#queryResHook").append(tmp);
	
	$("#delSN").val( sn );
	
	$.post("QueryDeliverAble",{ statusSN:statusSN,deliverSN_q:sn, tarSysNo:$("select option:selected").val() },function(reses) {
		if ( $(reses.rds).size() > 0  ) {	//需求下轄檔案裡有值
			
			tmp = "<h3>需求下轄程式</h3><table border='1' id='tableRDS'><tr><th>＃</th><th>需求編號</th><th>模組/檔案名稱</th><th>部署方式</th><th>來源<br>版次</th><th>本地<br>版次</th><th>資源群組</th></tr>";
			$("#hookRDS").append(tmp);
			reqNum = 1;		/*	需求數	*/
			preReqSN=0;		/*	前一個需求的需求ID	*/
			reqFileNum=1;	/*	一個需求下的檔案數	**/
			$.each(reses.rds,function(i, res) {
				$("#comment").val( res.comment );
				var bgcolor="#FFFFFF";
				if ((reqFileNum%2) ==0 ) {bgcolor="#CCCCCC"};
				var reqId = res.reqSN;
				var localVer = ((res.statusSN>2 && res.statusSN<200)?res.localVer:"&nbsp;");
				if (preReqSN == res.reqSN ) {	//還在同一個需求之下
					tmp = "<tr bgcolor='"+bgcolor+"'><td>"+res.localFile+"</td><td class='english_font'>"+res.depType+"</td><td class='english_font right'>"+res.localVer+"</td><td class='english_font right localVer' reqid='"+reqId+"' fn='"+res.localFile+"'>"+localVer+"</td><td><ul>";
				} else {
					tmp = "<tr><td class='req"+res.reqSN+" english_font center'>"+reqNum+"</td><td class='req"+res.reqSN+"' >"+res.reqID+"</td></tr><tr bgcolor='"+bgcolor+"'><td>"+res.localFile+"</td><td class='english_font'>"+res.depType+"</td><td class='english_font right'>"+res.localVer+"</td><td class='english_font right localVer' reqid='"+reqId+"' fn='"+res.localFile+"'>"+localVer+"</td><td><ul>";
					reqNum++;
				}
				$.each(res.sqs,function(j, sq) {
					tmp += "<li>"+sq.a+" ("+sq.d+") - "+sq.b+" ("+sq.e+")</li>";
					$("#subSysId").val( sq.a );	//供等會兒提領時，區別子系統別 (目前的設計其實重覆設值)
				});
				tmp += "</ul></td></tr>";
				$("#tableRDS").append(tmp);
				
				if (preReqSN != res.reqSN && (i+1)!=1 ) {	//已在不同個需求之下
					$(".req"+preReqSN).attr("rowspan",reqFileNum);
					reqFileNum=1;
				}
				reqFileNum++;				
				preReqSN = res.reqSN;
			});
			$(".req"+preReqSN).attr("rowspan",reqFileNum);
			$("#hookRDS").append("</table>");
		}
		if ( $(reses.gfs).size() > 0  ) {	//共用程式有值
			$.each(reses.gfs,function(i, res) {
				if ( $("#tableGFS").size() == 0) {	//共用程式Table的標頭 不存在，要先建立
					tmp = "<h3>共用程式</h3><table border='1' id='tableGFS'><tr><th>＃</th><th>需求編號</th><th colspan='2'>模組/檔案簡介</th><th>來源<br>版次</th><th>本地<br>版次</th><th>資源群組</th></tr></table>";
					$("#hookGFS").append(tmp);
				}
				
				tmp = "<ul>";
				$.each(res.sqs ,function(i, sq) {
					tmp += "<li>"+sq.a+" ("+sq.d+") - "+sq.b+" ("+sq.e+")</li>";
					$("#subSysId").val( sq.a );	//供等會兒提領時，區別子系統別 (目前的設計其實重覆設值)
				});
				tmp += "</ul>";
				
				rqLists = "<ol type='a'class='english_font'>";
				$.each(res.rqs ,function(i, res2) {
					rqLists += ("<li reqid='"+res2.a+"'>"+res2.b+"</li>");
				});
				rqLists += "</ol>";
				
				commFileNum = $(".commFileInd").size()+1;	
				
				tmp =	"<tr>"+
							"<td rowspan='3' class='commFileInd right english_font'>"+commFileNum+"</td>"+
							"<td rowspan='3' id='scfReqList'>"+rqLists+"</td>"+	//需求
							"<td class='right'>檔&nbsp;&nbsp;名&nbsp;&nbsp;：</td><td class='english_font' id='scfName'>"+ res.localFile +"</td>"+
							"<td rowspan='3' class='right english_font' id='souVer'>"+ res.localVer +"</td>"+	//來源版次
							"<td rowspan='3' class='right english_font localVer'></td>"+	//本地版次
							"<td rowspan='3' id='scfSqList'>"+ tmp +"</td>"+		//資源群組
					   	"</tr>"+
					   	"<tr bgcolor='#CCCCCC'>"+
							"<td class='right'>部署方式：</td><td class='english_font' id='scfDepType'>"+ res.depType +"</td>"+
						"</tr>"+
						"<tr bgcolor='#FFFFFF'>"+
							"<td class='right'>延伸路徑：</td><td class='english_font' id='scfDepPath'>"+ res.depPath +"</td>"+
						"</tr>";
				$("#tableGFS").append(tmp);
			});
		}
	});
	$("#wdButton").button();
}

$(document).ready(function() {
	
	if ($.cookie("userId") == null) {
		alert("請先登入系統");
		location.assign("index.html");
	} else {
		$(".dialog").css("visibility","hidden");
		
		$("#delSN").bind("keyup",function(){
			return validateNumber(this,this.value); 
		});
		
		$("#clickS").bind('click',function(){
			/* 確認欄位不為空	*/
			$("#removeQueryResHook").remove();
			tmp = "<div id='removeQueryResHook'><hr/><div id='removeQueryRes'><br><table id='deledList'><tr><th>&nbsp;</th><th>＃</th><th>程式來源</th><th>子系統</th><th>交付代碼</th><th>交付人員</th><th>狀態</th><th>提出時間</th></tr>";
			$("#queryResHook").append(tmp);
			
			$.post("QueryDevliver", {tarSysNo: $("#selectSysNo option:selected").val(), deliverSN_q: $("#delSN").val(),userId:$.cookie("userId"), isSU:$.cookie("isSU") },function(reses) {
				if ( reses.length > 0 ) {
					$.each(reses,function(i, res) {
						$("#hComment").val(res.comment);
						$("#statusSN").val(res.statusSN);
						if (i%2 == 1) { bgcolor="#CCCCCC";} else {bgcolor="#FFFFFF";}
						tmp = res.deliverySN;	
						disabled="";	
						if (res.statusSN !=2 ) { disabled="disabled='disabled'"; }
						tmp = "<tr bgcolor='"+bgcolor+"'><td><button type='button' "+disabled+" onclick=\"enDeliTask('"+tmp+"','"+res.statusSN+"')\">下一步</button></td><td class='english_font right'>"+(i+1)+"</td><td>"+$("#selectSysNo option:selected").html()+"</td><td>"+res.subSysName+"("+res.subSysId+")</td><td class='english_font right'>"+tmp+"</td><td class='english_font'>"+res.creatorName+"</td><td>"+res.statusdesc+"</td><td class='english_font'>"+res.createdDateTime+"</td></tr>";
						$("#deledList").append(tmp);
					});
				} else {
					$("#removeQueryRes").remove();
					$("#removeQueryResHook").append("<div class='alert'>查無資料</div>");
				}
			},"JSON");
			
			$("#deledList").append("</table></div></div>");
			$("button").button();
		});
		
		$.post("QuerySystemList",function(reses) {
			$.each(reses.ises,function(i, res) {
				tmp = "<option id='"+res.id+"' value='"+res.id+"'>"+res.name+"</option>";
				$("#selectSysNo").append(tmp);
			}); 
		},"JSON");
		
		initPage("提領程式");
	}
}).tooltip();
</script>

</head>
<body>

	<div id="header"></div>
		<table>
		<tr>
			<th class="right">選擇提領對象：</th>
			<td><select id='selectSysNo'></select></td>
		</tr>
		<tr>
			<th class="right">交付代碼：</th>
			<td><input type="text" id="delSN"></td>
		</tr>
		</table>
		
		<button id="clickS" type="button">查詢</button>
		
		<!-- <form id="q_form">
			<input type="hidden" id="deliverSN_q" name="deliverSN_q" >
			<input type="hidden" id="tarSysNo" name="tarSysNo" >
			<input type="hidden" id="statusSN" name="statusSN" >
			<input type="hidden" id="tarSysName" name="tarSysName" >
		</form> -->

	
	<p>
	<div id="queryResHook"></div>
	
	<p>
	<input type="hidden" id="hComment" >
	<input type="hidden" id="statusSN" >
	<input type="hidden" id="subSysId" >
	
	<div class="dialog" id="loading" ><label for="imgLoading" class="eyecatch" > Loading...</label><br/><img id='imgLoading' src='pic/ajax-loader.gif'></div>
	
</body>
</html>